package ProjectMain;

import GUI.System.GUIFrame;
import SQLDatabase.Managers.*;
import SerialCom.controller.EventManager;
import SerialCom.controller.PortDetection;
import SerialCom.protocol.ProjectPacket;
import SerialCom.serial.SerialTransceiver;
import java.util.TooManyListenersException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

/**
 * Create, initiate, and run the system. Use this for full integration and
 * acceptance testing.
 */
public class Main {

    public static void main(String[] args) {

        DatabaseManager databaseManager = new DatabaseManager();
        CustomerManager customerManager = new CustomerManager();
        TerminalManager terminalManager = new TerminalManager();
        BillingManager billingManager = new BillingManager();
        DepositManager depositManager = new DepositManager();

        customerManager.setDatabaseManager(databaseManager);
        terminalManager.setDatabaseManager(databaseManager);
        billingManager.setDatabaseManager(databaseManager);
        depositManager.setDatabaseManager(databaseManager);

        /* Check to see if an RS232 cable is connected. This allows for dynamic
         testing of GUI without the need for the micrcocontroller being 
         connected to the PC at the same time.
         */
        if (PortDetection.getPorts().isEmpty() == false) {
            EventManager eventManager = new EventManager();
            eventManager.setBillingManager(billingManager);
            eventManager.setCustomerManager(customerManager);
            eventManager.setTerminalManager(terminalManager);
            terminalManager.setEventManager(eventManager);

            SerialTransceiver transceiver = new SerialTransceiver(
                    new ProjectPacket(), eventManager);

            eventManager.setTransmitter(transceiver);

            System.out.println("TestMain: open port:");
            try {
                eventManager.openPort();
            } catch (TooManyListenersException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE,
                        null,
                        ex);
            }

            terminalManager.startPingSchedule();
        }

        /* -- Create the GUI front-end -- */
        /* Set the Nimbus look and feel  - generated by Netbeans */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info
                    : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUIFrame.class.getName()).log(
                    java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUIFrame.class.getName()).log(
                    java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUIFrame.class.getName()).log(
                    java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUIFrame.class.getName()).log(
                    java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        GUIFrame gui = new GUIFrame();

        gui.setDatabaseManager(databaseManager);
        gui.setBillingManager(billingManager);
        gui.setCustomerManager(customerManager);
        gui.setDepositManager(depositManager);
        gui.setTerminalManager(terminalManager);

        gui.setVisible(true);
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
